/**
 * Project Name: meatball-download-logs
 * Package Name: com.meatball.service
 * File Name: DownloadService
 * Date: 2021/2/2 22:44
 * User: 張翔宇
 * Copyright (c) 2021, zhang.xiangyu@foxmail.com All Rights Reserved.
 */
package com.meatball.service;

import com.meatball.SshRemoteCall;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.*;

/**
 *
 * @title: DownloadService.java
 * @author: 張翔宇
 * @date: 2021/2/2 22:44
 * @version: V3.0
 */
@Slf4j
@Service
public class DownloadService {
    private final Map<String, String> map = new HashMap<String, String>() {{
        put("172.17.184.138", "HejY6anZgDMRYONgBvFC");
        put("172.17.184.139", "NGWn49WtlfSBSjQwBNoz");
        put("172.17.184.140", "8t0Oxvwv1QkZVYVMJALA");
    }};
    /**
     * ip地址
     */
    private static String ipAddress = "182.92.75.11";
    /**
     * 账户
     */
    private static String userName = "root";
    /**
     * 密码
     */
    private static String password = "HejY6anZgDMRYONgBvFC";

    /**
     * 
     * @title: getDirectory 
     * @param directory 
     * @param ip
     * @return: java.util.List<java.lang.String> 返回类型 
     * @author: 張翔宇
     * @date: 2021/2/3 14:31 
     */
    public List<String> getDirectory(String directory, String ip) throws Exception {
        List<String> files = new ArrayList<>();
        // 1、首先远程连接ssh
        SshRemoteCall.getInstance().sshRemoteCallLogin(ip, userName, map.get(ip));
        // 打印信息
        log.info("0、连接服务器，ip地址：{}，账号：{}，连接成功", ipAddress, userName);
        // 展示目录下的文件信息
        String lsDirectory = "/home/workspace/logs/" + directory;
        Vector ls = SshRemoteCall.getInstance().listFiles(lsDirectory);
        ls.forEach(item -> {
            String line = item.toString();
            String name = line.substring(line.lastIndexOf(" ") + 1);
            if (!".".equals(name) && !"..".equals(name) && !"error".equals(name)) {
                files.add(name);
            }
        });
        Collections.sort(files);
        Collections.reverse(files);
        return files;
    }

    /**
     * 
     * @title: download
     * @param name 
     * @param directory 
     * @param ip
     * @return: void 返回类型 
     * @author: 張翔宇
     * @date: 2021/2/3 14:31 
     */
    public void download(String name, String directory, String ip, HttpServletResponse res) throws Exception {
        SshRemoteCall.getInstance().sshRemoteCallLogin(ip, userName, map.get(ip));
        // 打印信息
        log.info("0、连接服务器，ip地址：{}，账号：{}，连接成功", ipAddress, userName);
        String src = "/home/workspace/logs/" + directory + "/" + name;
        InputStream inputStream = SshRemoteCall.getInstance().fileDownload(src);
        // 发送给客户端的数据
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        // 读取filename
        bis = new BufferedInputStream(inputStream);
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
    }
}
